package com.linkoog.devtools.code_generator.wizard.generate.ddl;

import com.intellij.openapi.project.Project;
import com.linkoog.devtools.code_generator.database.domain.TableInfo;
import com.linkoog.devtools.code_generator.wizard.generate.database.table.panel.DbtableInfoContextPanel;
import com.linkoog.devtools.code_generator.wizard.GenerateWizardContext;
import com.intellij.openapi.options.ConfigurationException;
import com.linkoog.devtools.intellij.wizard.WizardContext;
import com.linkoog.devtools.intellij.wizard.WizardStep;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class TableInfoConfigStep extends WizardStep {

    private JPanel myMainPanel;
    private DbtableInfoContextPanel dbtableInfoContextPanel;

    public TableInfoConfigStep(Project project, WizardContext wizardContext) {
        super(project, wizardContext);
        this.dbtableInfoContextPanel = new DbtableInfoContextPanel();
        this.myMainPanel.add(dbtableInfoContextPanel.getMyMainPanel(), BorderLayout.CENTER);
    }


    public void resetTableInfoJTable(List<TableInfo> tableInfoList) {
        this.dbtableInfoContextPanel.resetTableInfoJTable(tableInfoList);
    }

    @Override
    public JComponent getComponent() {
        return myMainPanel;
    }

    @Override
    public void onStepPrepare() {
        GenerateWizardContext generateWizardContext = (GenerateWizardContext) getWizardContext();
        List<TableInfo> tableInfoList = generateWizardContext.getTableInfoList();
        resetTableInfoJTable(tableInfoList);
    }

    @Override
    public boolean validate() throws ConfigurationException {
        String error = null;

        error = dbtableInfoContextPanel.validate();
        if (error != null) {
            throw new ConfigurationException(error, "错误");
        }

        return true;
    }

    @Override
    public void onStepOK() {
        GenerateWizardContext generateWizardContext = (GenerateWizardContext) getWizardContext();
        generateWizardContext.setTableInfoList(dbtableInfoContextPanel.getSelectedDBTable());
    }


    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        myMainPanel = new JPanel();
        myMainPanel.setLayout(new BorderLayout(0, 0));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return myMainPanel;
    }
}
